package com.fd.cloud.fdftp.test;


import com.fd.cloud.fdftp.test.FtpUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("ftp")
public class FtpTestController {


    @RequestMapping("/test")
    public void FtpTest(HttpServletResponse response, MultipartFile file) throws IOException {
        //FileInputStream inputStream = file.getInputStream();
        //String fileName = file.getOriginalFilename();
        FileInputStream inputStream = new FileInputStream(new File("/Users/admin/Downloads/c.jpg"));
        //上传
        String path = "image";
        String fileName = FtpUtil.upload(inputStream, "1.png",path);

        System.out.println(fileName);

        //下载
        FtpUtil.downLoad(fileName,path,response);
    }


    @RequestMapping("/getFtpImage")
    public void getFtpImage(HttpServletResponse response, MultipartFile file) throws IOException {
        //FileInputStream inputStream = file.getInputStream();
        //String fileName = file.getOriginalFilename();
        FileInputStream inputStream = new FileInputStream(new File("C:/Users/admin/Desktop/1.png"));
        //上传
        String path = "image";
        String fileName = FtpUtil.upload(inputStream, "1.png",path);

        System.out.println(fileName);

        //查看图片
        String filePath = path+"/"+fileName;
        FtpUtil.getFtpImage(response,filePath);
    }



    @RequestMapping("/deleteFiles")
    public void deleteFiles() throws FileNotFoundException {
        //FileInputStream inputStream = file.getInputStream();
        //String fileName = file.getOriginalFilename();
        //FileInputStream inputStream = new FileInputStream(new File("/home/ftpftp/zwt/b.jpg"));
        //上传
        String path = "";
        //String fileName = FtpUtil.upload(inputStream, "1.png",path);

        String fileName1 ="/zwt/b.jpg";

        //删除
        List<String> files = new ArrayList<>();
        files.add(fileName1);
        FtpUtil.deleteFiles(path,files);
    }





    //多文件上传
    @RequestMapping(value = "/uploadFiles", method = RequestMethod.POST)
    public void fileUploads(HttpServletRequest request) throws IOException {
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        String path = "image";
        for (MultipartFile multipartFile : files) {
            if (!multipartFile.isEmpty()) {
                FtpUtil.upload((FileInputStream) multipartFile.getInputStream(), Objects.requireNonNull(multipartFile.getOriginalFilename()), path);
            }
        }
    }



}


